package com.ss.android.article.base.feature.detail2.article;

import android.content.SharedPreferences;
import android.os.AsyncTask;

import com.ss.android.article.base.feature.app.a.ArticleConstants;
import com.ss.android.article.base.utils.a.SharedPreferenceUtils;
import com.ss.android.common.AbsApiThread;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.common.util.UrlBuilder;
import com.ss.android.newmedia.util.InfoLRUCache;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class RecordManager {
    private static volatile RecordManager instance;
    Map<String, Integer> articleReadPositionRecordMap = new InfoLRUCache(128, 32);
    Map<String, NovelReadPositionRecord> serialRecordMap = new InfoLRUCache(128, 8);
    long mLastSaveRecordTs;
    private ExecutorService mExecutorService = Executors.newSingleThreadExecutor();

    private RecordManager() {
        new LoadLocalRecordAsyncTask().executeOnExecutor(this.mExecutorService, new Object[0]);
    }

    public static RecordManager getInstance() {
        if (RecordManager.instance == null) {
            synchronized (RecordManager.class) {
                if (RecordManager.instance == null) {
                    RecordManager.instance = new RecordManager();
                }
            }
        }
        return RecordManager.instance;
    }

    public static void reportReadPosition(long itemId, int articlePosition) {
        new ReportReadPositionThread(itemId, articlePosition).start();
    }

    public final void saveRecord(boolean cover) {
        if (cover) {
            new SaveRecordAsyncTask().executeOnExecutor(this.mExecutorService, this.getRecordArray());
        } else if (System.currentTimeMillis() - this.mLastSaveRecordTs > 0) {
            new SaveRecordAsyncTask().executeOnExecutor(this.mExecutorService, this.getRecordArray());
        }
    }

    private String[] getRecordArray() {
        String[] result = new String[2];
        JSONObject articleRecordJo = new JSONObject();
        try {
            Iterator<Map.Entry<String, Integer>> iterator = this.articleReadPositionRecordMap.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, Integer> entry = iterator.next();
                articleRecordJo.put(entry.getKey(), entry.getValue());
            }
        } catch (JSONException jsonException) {
            jsonException.printStackTrace();
        }

        JSONObject serialRecordJo = new JSONObject();
        try {
            Iterator<Map.Entry<String, NovelReadPositionRecord>> iterator = this.serialRecordMap.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, NovelReadPositionRecord> entry = iterator.next();
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("itemKey", ((NovelReadPositionRecord) ((Map.Entry) entry).getValue()).itemKey);
                jsonObject.put("record", ((NovelReadPositionRecord) ((Map.Entry) entry).getValue()).record);
                serialRecordJo.put(entry.getKey(), jsonObject);
            }
        } catch (JSONException jsonException) {
            jsonException.printStackTrace();
        }

        result[0] = articleRecordJo.toString();
        result[1] = serialRecordJo.toString();
        return result;
    }

    final class LoadLocalRecordAsyncTask extends AsyncTask {
        protected final Object doInBackground(Object[] arg5) {
            SharedPreferenceUtils.inst();
            String articleRecord = SharedPreferenceUtils.getString("sp_article_record", "sp_article_record@article_record", "");
            SharedPreferenceUtils.inst();
            return new String[]{articleRecord, SharedPreferenceUtils.getString("sp_article_record", "sp_article_record@serial_record", "")};
        }

        protected final void onPostExecute(Object arg9) {
            Object v0_1;
            Iterator v2;
            JSONObject v1;
            RecordManager.this.articleReadPositionRecordMap.clear();
            RecordManager.this.serialRecordMap.clear();
            String[] strArr = (String[]) arg9;
            try {
                v1 = new JSONObject(strArr[0]);
                v2 = v1.keys();
                while (v2.hasNext()) {
                    v0_1 = v2.next();
                    RecordManager.this.articleReadPositionRecordMap.put((String) v0_1, Integer.valueOf(v1.optInt(((String) v0_1))));
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }

            try {
                v1 = new JSONObject(strArr[1]);
                v2 = v1.keys();
                while (v2.hasNext()) {
                    v0_1 = v2.next();
                    JSONObject v3 = v1.optJSONObject(((String) v0_1));
                    RecordManager.this.serialRecordMap.put((String) v0_1, new NovelReadPositionRecord(v3.optString("itemKey"), v3.optInt("record")));
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            RecordManager.this.mLastSaveRecordTs = System.currentTimeMillis();
        }
    }

    final class SaveRecordAsyncTask extends AsyncTask {

        protected final Object doInBackground(Object[] params) {
            String[] strArr = (String[]) params;
            SharedPreferenceUtils.inst();
            SharedPreferences.Editor editor = SharedPreferenceUtils.getEditor("sp_article_record");
            if (params != null && params.length == 2) {
                editor.putString("sp_article_record@article_record", strArr[0]);
                editor.putString("sp_article_record@serial_record", strArr[1]);
            }
            editor.commit();
            return null;
        }

        protected final void onPostExecute(Object result) {
            RecordManager.this.mLastSaveRecordTs = System.currentTimeMillis();
        }
    }

    public static final class NovelReadPositionRecord {
        String itemKey;
        int record;

        public NovelReadPositionRecord(String itemKey, int record) {
            this.itemKey = itemKey;
            this.record = record;
        }
    }

    final static class ReportReadPositionThread extends AbsApiThread {
        private long itemId;
        private long articlePosition;

        ReportReadPositionThread(long itemId, int articlePosition) {
            this.itemId = itemId;
            this.articlePosition = ((long) articlePosition);
        }

        public final void run() {
            UrlBuilder builder = new UrlBuilder(ArticleConstants.articleReadPositionUrlV1);
            builder.addParam("item_id", this.itemId);
            builder.addParam("article_position", this.articlePosition);
            try {
                NetworkUtils.doGet(-1, builder.toString());
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }
    }
}

